package com.ycu.epidemic.service;

import com.ycu.epidemic.bean.DailyEpidemicInfo;
import com.ycu.epidemic.bean.EpidemicDetailInfo;
import com.ycu.epidemic.bean.EpidemicInfo;
import com.ycu.epidemic.bean.ProvinceInfo;
import com.ycu.epidemic.common.DateConverter;
import com.ycu.epidemic.mapper.EpidemicMapper;
import com.ycu.epidemic.mapper.ProvinceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EpidemicServiceImp implements EpidemicService{

    @Autowired
    private EpidemicMapper epidemicMapper;
    @Autowired
    private ProvinceMapper provinceMapper;

    @Override
    public List<ProvinceInfo> saveData(DailyEpidemicInfo dailyEpidemicInfo, Integer userId) {
        /**
        * 保存每日疫情数据
         * @Param dailyEpidemicInfo 每日疫情数据
         * @Param userId 录入用户ID
        * @Author:lijinhua
        * @Date:2020/7/12
        */
//        Date curDate = new Date();
        String curDate = "2020-7-12";//用上面的new Date实现保存每日数据

        String [] arr = dailyEpidemicInfo.getDate().split("-");
        short year=2020,month=7,day=12;
//        year = Short.parseShort(arr[0]);
//        month = Short.parseShort(arr[1]);
//        day = Short.parseShort(arr[2]);

        DateConverter dateConverter  = new DateConverter();
        Date curDte = dateConverter.convert(curDate);

        for (EpidemicInfo epidemicInfo:dailyEpidemicInfo.getArray()) {
            epidemicInfo.setUserId(userId);
            epidemicInfo.setInputDate(curDte);
            epidemicInfo.setDataYear(year);
            epidemicInfo.setDataMonth(month);
            epidemicInfo.setDataDay(day);
            epidemicMapper.saveInfo(epidemicInfo);
        }
        return provinceMapper.findNoDataProvince(year,month,day);
    }

    @Override
    public List<EpidemicDetailInfo> findLastestData() {
        /**
        * 查询每个省份累计疫情
         * @Param
        * @Author:lijinhua
        * @Date:2020/7/12
        */
        Calendar calendar = new GregorianCalendar();
        short year=2020,month=7,day=12;//注：日期此处写死了
//        year = (short) calendar.get(Calendar.YEAR);
//        month = (short) (calendar.get(Calendar.MONTH)+1);//0~11
//        day = (short) calendar.get(Calendar.DATE);
        Map<String,Short> condition = new HashMap<>();
        condition.put("year",year);
        condition.put("month",month);
        condition.put("day",day);
        return epidemicMapper.findLastestData(condition);
    }
}
